package com.resume.aipeople.repository;

import com.resume.aipeople.entity.Character;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface CharacterRepository extends JpaRepository<Character, Long> {
    
    Optional<Character> findByNameIgnoreCase(String name);
    
    List<Character> findByIsActiveTrue();
    
    List<Character> findByCategory(String category);
    
    @Query("SELECT c FROM Character c WHERE c.isActive = true AND " +
           "(LOWER(c.name) LIKE LOWER(CONCAT('%', :keyword, '%')) OR " +
           "LOWER(c.description) LIKE LOWER(CONCAT('%', :keyword, '%')) OR " +
           "LOWER(c.tags) LIKE LOWER(CONCAT('%', :keyword, '%')))")
    List<Character> searchCharacters(@Param("keyword") String keyword);
    
    @Query("SELECT c FROM Character c WHERE c.isActive = true ORDER BY c.chatCount DESC")
    List<Character> findMostPopularCharacters();
    
    @Query("SELECT DISTINCT c.category FROM Character c WHERE c.isActive = true")
    List<String> findAllCategories();
    
    @Modifying
    @Query("UPDATE Character c SET c.chatCount = c.chatCount + 1 WHERE c.id = :characterId")
    void incrementChatCountById(@Param("characterId") Long characterId);
} 